//
//  RequestTool.m
//  CarLifes
//
//  Created by yu on 2017/7/31.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "RequestTool.h"
#import "EquipmentModel.h"
#import "TraceModel.h"
#import "AlarmModel.h"
#import "CityModel.h"
#import "QueryResultModel.h"
#import "NewsModel.h"
#import "WorkorderModel.h"
#import "QuestionModel.h"
#import "AFNetworking.h"
#import "HighVioModel.h"
#import "DepartmentModel.h"

#define STATUS @"status"
#define RESULT @"result"

@implementation RequestTool

/** 获取下级部门*/
- (void)findDeptsByDeptId:(NSString *)deptId success:(void (^)(NSArray *depts))success failure:(void (^)(NSString *error))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kFindDept);
    [HTTPRequest postRequestWithURL:url parameters:@{@"deptId":deptId} show:YES success:^(id response) {
        PPLog(@"depts下级部门 == %@",response);
        NSArray *depts = [DepartmentModel mj_objectArrayWithKeyValuesArray:response[RESULT]];
        success(depts);
    } failure:^(NSError *error) {
        failure(error.description);
    }];
}

/** 查询违章高发地*/
- (void)loadHighVioByLat:(NSString *)lat lng:(NSString *)lng num:(NSString *)num range:(NSString *)range success:(void (^)(NSMutableArray *vios))success failure:(void (^)(NSString *error))failure {
    NSString *appcode = @"8035f816dc954c4b8206b61058b53dd7";
    NSString *host = @"https://jisuwzgfd.market.alicloudapi.com";
    NSString *path = @"/illegaladdr/coord";
    NSString *querys = NSStringFormat(@"?lat=%@&lng=%@&num=10&range=3000",lat,lng);
    NSString *url = [NSString stringWithFormat:@"%@%@%@",host,path,querys];

//    [PPNetworkHelper setResponseSerializer:PPResponseSerializerJSON];
    [HTTPRequest setValue:NSStringFormat(@"APPCODE %@",appcode) forHTTPHeaderField:@"Authorization"];
    [HTTPRequest getRequestWithURL:url parameters:nil show:YES success:^(id response) {
        PPLog(@"%@",response);
        PPLog(@"%@",[response[STATUS] class]);
        if ([response[STATUS] isEqualToNumber:@(0)]) {
            NSMutableArray *vios = [HighVioModel mj_objectArrayWithKeyValuesArray:response[RESULT]];
            success(vios);
        }
    } failure:^(NSError *error) {
        failure(error.description);
    }];
}

/** 上传安装师傅位置*/
- (void)uploadInstallerLocationByInstallId:(NSString *)installId lat:(NSString *)lat lng:(NSString *)lng success:(void (^)())success failure:(void (^)(NSString *))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kUploadInstallerLocation);
    NSDictionary *params = @{@"installId":installId,@"lat":lat,@"lng":lng};
    [HTTPRequest postRequestWithURL:url parameters:params show:NO success:^(id response) {
        PPLog(@"upload installer location = %@",response);
        success();
    } failure:^(NSError *error) {
        PPLog(@"error = %@",error.description);
        failure(error.description);
    }];
}

/** 上传家访图片*/
- (void)uploadWoVisImgByWoId:(NSString *)woId images:(NSArray <UIImage *> *)images success:(void (^)(int))success failure:(void (^)(NSString *error))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kUploadWoVisImg);
    [HTTPRequest uploadImagesWithURL:url parameters:@{@"woId":woId} name:@"img" images:images imageScale:0.08 imageType:@"png" progress:^(CGFloat progress) {
        PPLog(@"uploadWoVisImageProgress == %lf",progress);
//        progresses(progress);
    } success:^(id response) {
        PPLog(@"*********%@",[self jsonToString:response]);
        success([response[STATUS] intValue]);
    } failure:^(NSError *error) {
        PPLog(@"error>>>>>%@",error.description);
        failure(error.description);
    }];
}

/** 家访工单提交题目*/
- (void)workorderUpdateVisStatus:(NSString *)str success:(void (^)(BOOL isFinish))success failure:(void (^)(NSString *error))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kWorkorderUpdateVisStatus);
    [HTTPRequest postRequestWithURL:url parameters:@{@"str":str} show:YES success:^(id response) {
//        PPLog(@"submit answer == %@",response);
        if ([response[STATUS] isEqual:@(0)]) {
            success(YES);
        } else {
            success(NO);
        }
    } failure:^(NSError *error) {
        failure(error.description);
    }];
}

/** 查询家访工单题目*/
- (void)loadVisitingQuestionByVisId:(NSString *)visId visType:(NSString *)visType success:(void (^)(NSMutableArray *))success failure:(void (^)(NSString *))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kWorkorderQuestion);
    NSDictionary *params = @{@"visId":visId,@"visType":visType};
    [HTTPRequest postRequestWithURL:url parameters:params show:YES success:^(id response) {
        PPLog(@"load visiting question == %@",response);
        NSMutableArray *questionList = [QuestionModel mj_objectArrayWithKeyValuesArray:response[RESULT]];
        success(questionList);
    } failure:^(NSError *error) {
        failure(error.description);
    }];
}

/** 安装工单完结(修改工单状态)*/
- (void)workorderUpdateStatus:(NSString *)woId status:(NSString *)status workEqnos:(NSString *)workEqnos success:(void (^)(BOOL isFinish))success failure:(void (^)(NSString *))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kUpdateWorkorderStatus);
    NSDictionary *params = @{@"woId":woId,@"status":status,@"workEqnos":workEqnos};
    [HTTPRequest postRequestWithURL:url parameters:params show:YES success:^(id response) {
        if ([response[STATUS] isEqual:@(0)]) {
            success(YES);
        } else {
            success(NO);
        }
    } failure:^(NSError *error) {
        failure(error.description);
    }];
}

/** 工单查询*/
- (void)workorderFind:(NSString *)installerId page:(NSInteger)page status:(NSString *)status str:(NSString *)str success:(void (^)(NSMutableArray *))success failure:(void (^)(NSString *))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kWorkorderFind);
    NSDictionary *params = @{@"installerId":installerId,@"page":@(page),@"size":@(10),@"status":status,@"str":str};
    [HTTPRequest postRequestWithURL:url parameters:params show:YES success:^(id response) {
        PPLog(@"workorderFindList == %@",[self jsonToString:response]);
        if ([response[STATUS] isEqual:@(0)]) {
            NSMutableArray *workorderList = [WorkorderModel mj_objectArrayWithKeyValuesArray:response[RESULT][@"items"]];
            success(workorderList);
        } else {
            success(nil);
        }
    } failure:^(NSError *error) {
        PPLog(@"workorder find error == %@",error.description);
        failure(error.description);
    }];
}

/** 工单登录系统*/
- (void)workorderLogin:(NSString *)account isSaveAccount:(BOOL)isSave success:(void (^)(int,NSString *))success failure:(void (^)(NSError *))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kWorkorderLogin);
    [HTTPRequest postRequestWithURL:url parameters:@{@"phoneNo":account} show:YES success:^(id response) {
        if (isSave) {
            self.user.workorderAccount = account;
        } else {
            self.user.workorderAccount = @"";
        }
        PPLog(@"workorder login response == %@",response);
        if ([response[STATUS] isEqual:@(0)]) {
            success(0,response[RESULT][@"installId"]);
        } else {
            success(1,nil);
        }
    } failure:^(NSError *error) {
        failure(error);
    }];
}

/** 新闻资讯*/
- (void)loadNews:(NSString *)type show:(BOOL)show success:(void (^)(NSMutableArray *))success failure:(void (^)(NSString *))failure {
    NSString *appcode = @"e6451fbe968e4fbb8a8b41e3848138c6";
    NSString *host = @"http://toutiao-ali.juheapi.com";
    NSString *path = @"/toutiao/index";
    NSString *querys = NSStringFormat(@"?type=%@",type);
    NSString *url = [NSString stringWithFormat:@"%@%@%@",host,path,querys];
    
    [PPNetworkHelper setResponseSerializer:PPResponseSerializerJSON];
    [HTTPRequest setValue:NSStringFormat(@"APPCODE %@",appcode) forHTTPHeaderField:@"Authorization"];
    [HTTPRequest getRequestWithURL:url parameters:nil show:show success:^(id response) {
        if ([response[RESULT] isKindOfClass:[NSNull class]]) {
            success(nil);
        } else {
            NSMutableArray *newsList = [NewsModel mj_objectArrayWithKeyValuesArray:response[RESULT][@"data"]];
            success(newsList);
        }
    } failure:^(NSError *error) {
        failure(error.description);
    }];
}

/** 违章查询*/
- (void)queryVehicleInfoByCity:(NSString *)city hphm:(NSString *)hphm hpzl:(NSString *)hpzl engineno:(NSString *)engineno classno:(NSString *)classno success:(void (^)(NSMutableArray *,NSString *))success failure:(void (^)(NSString *))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kQueryVehicleInfo);
    NSDictionary *params = @{@"city":city,
                             @"hphm":hphm,
                             @"hpzl":hpzl,
                             @"engineno":engineno,
                             @"classno":classno
                             };
    AFHTTPSessionManager *_sessionManager = [AFHTTPSessionManager manager];
    // 设置请求的超时时间
    _sessionManager.requestSerializer.timeoutInterval = 30.f;
    // 设置服务器返回结果的类型:JSON (AFJSONResponseSerializer,AFHTTPResponseSerializer)
    _sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    _sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/plain", @"text/javascript", @"text/xml", @"image/*", nil];
    [_sessionManager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *response = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        PPLog(@"result$$$ == %@",response);
        
        if ([response hasPrefix:@"2"]) {
            NSString *tip = [response substringFromIndex:7];
            PPLog(@"%@",tip);
            success(nil,tip);
        }
        
        if ([response hasPrefix:@"{"]) {
            NSData *jsonData = [response dataUsingEncoding:NSUTF8StringEncoding];
            NSError *error;
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
            PPLog(@"dict = %@",dic);
            NSMutableArray *resultList = [QueryResultModel mj_objectArrayWithKeyValuesArray:dic[@"lists"]];
            success(resultList,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        PPLog(@"error = %@",error.description);
    }];
}

/** 获取违章城市*/
- (void)loadAllCitySuccess:(void (^)(NSMutableArray *))success failure:(void (^)(NSString *))failure {
//        NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kFetchAllCity);
    NSString *url = @"http://v.juhe.cn/wz/citys?province=&dtype=&format=&callback=&key=e48046ba7ff876ddc5fb638a76209e6c";
    [HTTPRequest postRequestWithURL:url parameters:nil show:YES success:^(id response) {
        NSDictionary *dic = response[RESULT];
        NSMutableArray *cities = [NSMutableArray array];
        for (NSString *key in [dic allKeys]) {
            NSDictionary *dict = dic[key];
            NSMutableArray *cityList = [CityModel mj_objectArrayWithKeyValuesArray:dict[@"citys"]];
            [cities addObjectsFromArray:cityList];
        }
        success(cities);
    } failure:^(NSError *error) {
        failure(error.description);
    }];
}

/** 报警查询*/
- (void)loadAlarmListByPage:(NSInteger)page eqNos:(NSString *)eqNos egId:(NSString *)egId success:(void (^)(NSArray *))success failure:(void (^)(NSError *))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kAlarmSearch);
    NSString *deptId = egId.length?egId:self.user.deptModel.deptId;
    
    NSDictionary *params = @{@"deptId":deptId,
                             @"page":[@(page) stringValue],
                             @"size":[@(10) stringValue],
                             @"eqNos":eqNos};
    [HTTPRequest postRequestWithURL:url parameters:params show:YES success:^(id response) {
        PPLog(@"alarm response = %@",response);
        if ([response[STATUS] isEqual:@(0)]) {
            NSArray *alarmArray = [AlarmModel mj_objectArrayWithKeyValuesArray:response[RESULT][@"items"]];
            success(alarmArray);
        } else {
            success(@[]);
        }
    } failure:^(NSError *error) {
        failure(error);
    }];
}

- (void)loadAlarmsByPage:(NSInteger)page eqNos:(NSString *)eqNos egId:(NSString *)egId success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kAlarmSearch);
    // 车架号.设备号登录，查询报警用egId，帐号登录查报警用deptId
    NSString *deptId = egId.length ? egId : self.user.deptModel.deptId;
    NSDictionary *params = @{@"deptId":deptId,
                             @"page":[@(page) stringValue],
                             @"size":[@(10) stringValue],
                             @"eqNos":eqNos};
    [HTTPRequest postRequestWithURL:url parameters:params show:YES success:^(id response) {
        PPLog(@"alarm response = %@",[self jsonToString:response]);
        if ([response[STATUS] isEqual:@(0)]) {
            NSArray *alarmArray = [AlarmModel mj_objectArrayWithKeyValuesArray:response[RESULT][@"items"]];
            success(alarmArray);
        } else {
            success(@[]);
        }
    } failure:^(NSError *error) {
        failure(error.description);
    }];
}

/** 意见反馈*/
- (void)feedbackByInfo:(NSString *)info contact:(NSString *)contact phone:(NSString *)phone app:(NSString *)app success:(void (^)())success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@%@",kApiPrefix,kFeedback];
    NSDictionary *params = @{@"userId":self.user.userId,@"info":info,@"contact":contact,@"phone":phone,@"app":app};
    [HTTPRequest postRequestWithURL:url parameters:params show:YES success:^(id response) {
        if ([response[STATUS] isEqual:@0]) {
            success();
        }
    } failure:^(NSError *error) {
        failure(error);
    }];
}

/** 帐号登录*/
- (void)loginByAccount:(NSString *)account password:(NSString *)password isSavePwd:(BOOL)isSave success:(void (^)(int))success failure:(void (^)(NSString *))failure {
    NSString *url = [NSString stringWithFormat:@"%@%@",kApiPrefix,kLogin];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"loginName"] = account;
    params[@"password"] = password;
    
    [HTTPRequest postRequestWithURL:url parameters:params show:YES success:^(id response) {
        PPLog(@"登录 respon==%@",response);
#warning 需要处理此处！！！
        if ([response[STATUS] isEqual:@0]) {
            NSDictionary *result = response[RESULT];
            
            if (![result[@"efDepartment"] isKindOfClass:[NSNull class]]) {
                self.user.userId = NSStringFormat(@"%@",result[@"userId"]);
                self.user.nickName = NSStringFormat(@"%@",result[@"userName"]);
                self.user.deptModel.deptId = NSStringFormat(@"%@",result[@"efDepartment"][@"deptId"]);
                self.user.deptModel.childDeptId = NSStringFormat(@"%@",result[@"efDepartment"][@"deptId"]);
                self.user.deptModel.parentDeptId = NSStringFormat(@"%@",result[@"efDepartment"][@"parentDeptId"]);
                self.user.deptName = NSStringFormat(@"%@",result[@"efDepartment"][@"deptName"]);
                success(0);
            } else {
                success(1);
            }
        } else {
            success(1);
        }
        // 如果为YES,保存帐号密码，否则必须！清空本地保存的帐号密码.
        if (isSave) {
            self.user.account = account;
            self.user.password = password;
        } else {
            self.user.account = @"";
            self.user.password = @"";
        }
    } failure:^(NSError *error) {
        PPLog(@"%@",error.description);
        failure(error.description);
    }];
}

- (void)updateUserInfoName:(NSString *)name email:(NSString *)email mobile:(NSString *)mobile success:(void (^)())success failure:(void (^)(NSError *))failure
{
    NSString *url = [NSString stringWithFormat:@"%@%@",kApiPrefix,kUpdateUserInfo];
    NSDictionary *params = @{@"userId":self.user.userId,@"userName":name,@"userEmail":email,@"cellPhone":mobile};
    [HTTPRequest postRequestWithURL:url parameters:params show:YES success:^(id response) {
//        PPLog(@"modify user info  ==  %@",response);
        if ([response[STATUS] isEqual:@0])
        {
            if (![name isEqualToString:@""]) {
                self.user.nickName = response[RESULT][@"userName"];
            }
            if (![email isEqualToString:@""]) {
                self.user.email = response[RESULT][@"userEmail"];
            }
            if (![mobile isEqualToString:@""]) {
                self.user.mobile = response[RESULT][@"cellPhone"];
            }
        }
        success();
    } failure:^(NSError *error) {
        failure(error);
    }];
}

- (void)modifyUserPassword:(NSString *)newPassword success:(void (^)())success failure:(void (^)(NSError *))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kModifyAccountPw);
    [HTTPRequest postRequestWithURL:url parameters:@{@"userId":self.user.userId,@"password":newPassword} show:YES success:^(id response) {
        if ([response[STATUS] isEqual:@0]) {
            success();
        }
    } failure:^(NSError *error) {
        failure(error);
    }];
}

/** 修改账户密码*/
- (void)changeAccPsd:(NSString *)psd isSavePsd:(BOOL)isSave success:(void (^)(void))success failure:(void (^)(NSString *))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kChangeAccPsd);
    [HTTPRequest postRequestWithURL:url parameters:@{@"userId":self.user.userId,@"password":psd} show:YES success:^(id response) {
        PPLog(@"change account psd = %@",response);
        if ([response[STATUS] isEqual:@0]) {
            success();
            self.user.password = psd;
        }
    } failure:^(NSError *error) {
        failure(error.description);
    }];
}

/** 修改车架号登录密码*/
- (void)changeVinPsd:(NSString *)psd eqId:(NSString *)eqId success:(void (^)(void))success failure:(void (^)(NSString *))failure
{
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kChangeVinPsd);
    [HTTPRequest postRequestWithURL:url parameters:@{@"eqId":eqId,@"password":psd} show:YES success:^(id response) {
        PPLog(@"change vin psd = %@",response);
        if ([response[STATUS] isEqual:@0]) {
            success();
            self.user.vinPassword = psd;
        }
    } failure:^(NSError *error) {
        failure(error.description);
    }];
}

/** 平台列表(搜索设备、获取帐号设备、获取用户信息公用接口)*/
- (void)loadEquipmentListByPage:(NSInteger)page size:(NSInteger)size status:(NSString *)status actStatus:(NSString *)actStatus str:(NSString *)str isNeedDeptId:(BOOL)isNeedDeptId show:(BOOL)show success:(void (^)(NSArray *,NSArray *))success failure:(void (^)(NSError *))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kUserDept);
    NSString *deptId = isNeedDeptId?self.user.deptModel.deptId:@"";
    NSDictionary *params = @{
                             @"page":[@(page) stringValue],
                             @"size":[@(size) stringValue],
                             @"status":status,
                             @"actStatus":actStatus,
                             @"str":str,
                             @"deptId":deptId
                             };
    
    [HTTPRequest postRequestWithURL:url parameters:params show:show success:^(id response) {
        if ([response[STATUS] isEqual:@0]) {
            NSArray *equipList = [EquipmentModel mj_objectArrayWithKeyValuesArray:response[RESULT][0][@"items"]];
            EquipStatusCount *equipCounts = [EquipStatusCount mj_objectWithKeyValues:response[RESULT][1]];
            success(equipList,@[equipCounts]);
        } else {
            success(@[],@[]);
        }
    } failure:^(NSError *error) {
        failure(error);
    }];
}

- (void)loadEquipsByPage:(NSInteger)page size:(NSInteger)size status:(NSString *)status actStatus:(NSString *)actStatus str:(NSString *)str deptId:(NSString *)deptId show:(BOOL)show success:(void (^)(NSArray *, NSArray *))success failure:(void (^)(NSString *))failure {
    
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kUserDept);
//    NSString *deptId = isNeedDeptId?self.user.deptModel.deptId:@"";
    NSDictionary *params = @{
                             @"page":[@(page) stringValue],
                             @"size":[@(size) stringValue],
                             @"status":status,
                             @"actStatus":actStatus,
                             @"str":str,
                             @"deptId":deptId
                             };
    
    [HTTPRequest postRequestWithURL:url parameters:params show:show success:^(id response) {
        PPLog(@"res***** = %@",[self jsonToString:response]);
        if ([response[STATUS] isEqual:@(0)]) {
            NSArray *equipList = [EquipmentModel mj_objectArrayWithKeyValuesArray:response[RESULT][0][@"items"]];
            EquipStatusCount *equipCounts = [EquipStatusCount mj_objectWithKeyValues:response[RESULT][1]];
            success(equipList,@[equipCounts]);
        } else {
            success(@[],@[]);
        }
    } failure:^(NSError *error) {
        failure(error.description);
    }];
}

- (void)loadEquipmentStatusStr:(NSString *)str success:(void (^)(NSArray *eqList))success failure:(void (^)(NSError *error))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kUserDept);
    NSDictionary *params = @{
                             @"page":@"1",
                             @"size":@(9999999),
                             @"status":@"",
                             @"actStatus":@"",
                             @"str":str,
                             @"deptId":@""
                             };
    [HTTPRequest postRequestWithURL:url parameters:params show:NO success:^(id response) {
        if ([response[STATUS] isEqual:@0]) {
            NSArray *equipList = [EquipmentModel mj_objectArrayWithKeyValuesArray:response[RESULT][0][@"items"]];
            success(equipList);
        } else {
            success(nil);
        }
    } failure:^(NSError *error) {
        failure(error);
    }];
}

/** 车架号登录*/
- (void)loginByVin:(NSString *)vin password:(NSString *)password isSavePwd:(BOOL)isSave success:(void (^)(int,NSArray *equipmentList))success failure:(void (^)(NSError *))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kVinLogin);
    NSDictionary *params = @{@"vin":vin,@"password":password};
    [HTTPRequest postRequestWithURL:url parameters:params show:YES success:^(id response) {
        PPLog(@"Vin Login = %@",[self jsonToString:response]);
        if ([response[STATUS] isEqual:@0]) {
            NSArray *equipList = [EquipmentModel mj_objectArrayWithKeyValuesArray:response[RESULT]];
            PPLog(@"equipList == %ld",equipList.count);
            success([response[@"status"] intValue],equipList);
        } else {
            success(1,nil);
        }
        // 如果为YES,保存帐号密码，否则必须清空本地保存的帐号密码.
//        if (isSave) {
            self.user.vinAccount = vin;
            self.user.vinPassword = password;
//        } else {
//            self.user.vinAccount = @"";
//            self.user.vinPassword = @"";
//        }
    } failure:^(NSError *error) {
        failure(error);
    }];
}

/** 上传安装拍照照片*/
- (void)uploadImagesByEqId:(NSString *)eqId installer:(NSString *)installer installerPhone:(NSString *)installerPhone installerPosition:(NSString *)installerPosition images:(NSArray<UIImage *> *)images eqPosition:(NSString *)eqPosition success:(void (^)(int))success failure:(void (^)(NSError *))failure
{
    NSString *url = [NSString stringWithFormat:@"%@%@",kApiPrefix,kUploadImg];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"eqId"] = eqId;
    params[@"installer"] = installer;
    params[@"installerPhone"] = installerPhone;
    params[@"installerPosition"] = installerPosition;
    params[@"eqPosition"] = eqPosition;
    
    [HTTPRequest uploadImagesWithURL:url parameters:params name:@"img" images:images imageScale:0.08 imageType:@"png" progress:^(CGFloat progress) {
        
    } success:^(id response) {
        success([response[STATUS] intValue]);
        PPLog(@"upload images success = %@",[self jsonToString:response]);
    } failure:^(NSError *error) {
        PPLog(@"upload images failure = %@",error);
        failure(error);
    }];
}

/** 设备拍照查询*/
- (void)queryEquipImgByEqId:(NSString *)eqId show:(BOOL)show success:(void (^)(NSArray *))success failure:(void (^)(NSError *))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kFindImg);
    [HTTPRequest postRequestWithURL:url parameters:@{@"eqId":eqId} show:show success:^(id response) {
        PPLog(@"find img response == %@",[self jsonToString:response]);
        NSString *status = [response[STATUS] stringValue];
        if ([status isEqualToString:@"0"]) {
            success(response[RESULT]);
        } else {
            success(nil);
        }
        
    } failure:^(NSError *error) {
        failure(error);
    }];
}

/** 上传补拍照片*/
- (void)uploadRepearImgByEqId:(NSString *)eqId img:(UIImage *)img type:(NSString *)type success:(void (^)(int status))success failure:(void (^)(NSError *error))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kRepeatImg);
    [HTTPRequest uploadImageWithURL:url parameters:@{@"eqId":eqId,@"type":type} name:@"img" images:@[img] imageScale:0.08 imageType:@"png" progress:^(CGFloat progress) {
        
    } success:^(id response) {
        PPLog(@"repeat img response == %@",response);
        success([response[STATUS] intValue]);
    } failure:^(NSError *error) {
        PPLog(@"repeat img error == %@",error.description);
        failure(error);
    }];
}

/** 上传补拍照片666*/
- (void)uploadRepearImgByEqId666:(NSString *)eqId images:(NSArray<UIImage *> *)images types:(NSArray<NSString *> *)types success:(void (^)(int status))success failure:(void (^)(NSError *error))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kRepeatImg);
    [HTTPRequest uploadImageWithURL666:url eqId:eqId types:types parameters:nil name:@"img" images:images imageScale:0.08 imageType:@"png" success:^(int status) {
        success(status);
    } failure:^(NSError *error) {
        failure(error);
    }];
}

/** 设备号登录(安装拍照登录)*/
- (void)eqNoLoginByEqno:(NSString *)eqno success:(void (^)(EqnoLoginModel *,int))success failure:(void (^)(NSError *))failure {
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kEqnoLogin);
    [HTTPRequest postRequestWithURL:url parameters:@{@"eqNo":eqno} show:YES success:^(id response) {
        if ([response[STATUS] isEqual:@0]) {
            EqnoLoginModel *eqnoModel = [EqnoLoginModel mj_objectWithKeyValues:response[RESULT]];
            success(eqnoModel,0);
        } else {
            success(nil,[response[STATUS] intValue]);
        }
    } failure:^(NSError *error) {
        failure(error);
    }];
}

/** 查询历史轨迹*/
- (void)loadHistoryTrackByEqno:(NSString *)eqno startTime:(NSString *)startTime endTime:(NSString *)endTime success:(void (^)(NSArray *,int))success failure:(void (^)(NSError *))failure {
    
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kHistoryTrack);
    
    [HTTPRequest postRequestWithURL:url parameters:@{@"eqNo":eqno,@"startTime":startTime,@"endTime":endTime} show:YES success:^(id response) {
//        PPLog(@"trace point = %@",response);
        if ([response[STATUS] isEqual:@0]) {
            NSArray *historyTracks = [TraceModel mj_objectArrayWithKeyValuesArray:response[RESULT]];
            success(historyTracks,0);
//            PPLog(@"history point count = %ld",historyTracks.count);
        } else {
            success(nil,[response[STATUS] intValue]);
        }
    } failure:^(NSError *error) {
        PPLog(@"trace point error = %@",error);
        failure(error);
    }];
}

/** 查询实时轨迹*/
- (void)loadRealTrackByEqno:(NSString *)eqno show:(BOOL)show success:(void (^)(TraceModel *))success failure:(void (^)(NSError *))failure {
    
    NSString *url = NSStringFormat(@"%@%@",kApiPrefix,kRealTrack);
    
    [HTTPRequest postRequestWithURL:url parameters:@{@"eqNo":eqno} show:show success:^(id response) {
        if ([response[STATUS] isEqual:@0]) {
            TraceModel *traceModel = [TraceModel mj_objectWithKeyValues:response[RESULT][0]];
            success(traceModel);
        }
    } failure:^(NSError *error) {
        failure(error);
    }];
}


- (NSString *)jsonToString:(id)data {
    if(data == nil) { return nil; }
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:nil];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

- (User *)user {
    if (!_user) {
        _user = [User sharedUser];
    }
    return _user;
}

@end
